<template>
	<view class="add-game-container">
		<!-- 搜索栏 -->
		<view class="search-bar">
			<u-search placeholder="请输入搜索内容" v-model="keyword" shape="square" animation="false" height="40"></u-search>
		</view>

		<view class="game-list">
			<view class="game-item" v-for="(item, index) in gameList" :key="item.name">
				<!-- <view class="rank">#{{ index + 1 }}</view> -->
				<image :src="item.picture" class="game-image"></image>
				<view class="game-info">
					<view class="game-name">
						{{ item.name }}
						<u-tag v-if="!item.selected" :text="item.tag" size="mini" :bgColor="themeColor" :borderColor="themeColor"></u-tag>
						<u-tag v-else text="已选择" size="mini" bgColor="#4cd964" borderColor="#4cd964"></u-tag>
						<view v-if="!item.selected" class="add-game">
							<u-icon name="plus-circle-fill" :color="themeColor" size="24" @click="handlePushGameList(item, index)"></u-icon>
						</view>
					</view>
					<u-rate :value="item.point / 2" :active-color="themeColor" inactive-color="#999" size="14" readonly allowHalf></u-rate>
					<view class="game-desc">
						<view class="game-desc-item"><u-icon name="tags" color="#999" size="14"></u-icon>{{ item.difficulty }}</view>
						<view class="game-desc-item"><u-icon name="account" color="#999" size="14"></u-icon>{{ item.players }}</view>
						<view class="game-desc-item"><u-icon name="clock" color="#999" size="14"></u-icon>{{ item.time }}</view>
					</view>
				</view>
			</view>
		</view>

		<!-- 添加游戏栏 -->
		<view class="add-game-bar">
			<image src="@/static/dice.png" class="add-game-icon" />
			<view>选择{{ playGameList.length }}款桌游</view>
			<view class="confirm-button" @click="handleSubmit">
				<view class="confirm-button-top">选好了</view>
				<view class="confirm-button-bottom">Finish</view>
			</view>
			<!-- <u-button type="primary" shape="circle" icon="plus" @click="toAddGame">添加游戏</u-button> -->
		</view>
	</view>
</template>

<script>
import request from '@/api/http.js';

export default {
	data() {
		return {
			gameList: [],
			mode: 'CreateActivity',
			playGameList: [] // 已选择的游戏列表
		};
	},
	onShow() {
		this.getGameList();
	},
	methods: {
		async getGameList() {
			const difficultyMap = {
				EASY: '简单',
				MEDIUM: '中等',
				HARD: '困难'
			};
			const res = await request(`/game/page`);
			let gameList = res.data.list;
			// 全部游戏
			gameList = gameList.map((item) => {
				return {
					...item,
					difficulty: difficultyMap[item.difficulty],
					tag: item.types.split('|')[0],
					selected: false
				};
			});
			this.gameList = gameList;
		},
		handleSubmit() {
			// 回到上一页
			const pages = getCurrentPages();
			const prevPage = pages[pages.length - 2];
			prevPage.$vm.playGameList = this.playGameList;
			uni.navigateBack({
				delta: 1
			});
		},
		handlePushGameList(item, index) {
			if (!this.gameList[index].selected) {
				this.playGameList.push(item);
				this.gameList[index].selected = true;
			}
		}
	}
};
</script>

<style lang="scss">
.add-game-container {
	position: relative;
}

.search-bar {
	padding: 20rpx;
}

.game-list {
	display: flex;
	flex-direction: column;
	padding: 20rpx;
	gap: 10rpx;

	.game-item {
		margin-bottom: 20rpx;
		display: flex;
		align-items: flex-start;
		padding: 20rpx;
		border-bottom: 1px solid #eee;

		.rank {
			color: $theme-color;
			font-size: 48rpx;
			font-weight: bold;
			margin: 0 10rpx;
		}

		.game-image {
			width: 150rpx;
			height: 150rpx;
			border-radius: 25rpx;
			object-fit: contain;
			margin: 0 10rpx;
		}

		.game-info {
			flex: 1;
			height: 150rpx;
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			padding: 0 20rpx;
			font-size: $font-size-base;
			gap: 10rpx;

			.game-name {
				font-size: 32rpx;
				font-weight: bold;
				display: flex;
				align-items: center;
				gap: 10rpx;

				.add-game {
					margin-left: auto;
					&:active {
						opacity: 0.8;
					}
				}
			}

			.game-desc {
				display: flex;
				color: #999;
				gap: 15rpx;

				.game-desc-item {
					display: flex;
					align-items: center;
					gap: 10rpx;
				}
			}
		}
	}
}

.add-game-bar {
	position: fixed;
	bottom: 20rpx;
	left: 0;
	right: 0;
	margin: 0 auto;
	display: flex;
	align-items: center;
	width: 550rpx;
	height: 50rpx;
	background-color: $theme-color;
	border-radius: 16rpx;
	box-shadow: 0 0 10rpx rgba(0, 0, 0, 0.1);
	padding: 20rpx;
	z-index: 999;
	color: #fff;
	font-size: $font-size-base;
	gap: 20rpx;

	.add-game-icon {
		width: 50rpx;
		height: 50rpx;
	}

	.confirm-button {
		margin-left: auto;
		margin-right: 10rpx;
		font-size: 32rpx;
		text-align: center;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		gap: 0rpx;

		&-bottom {
			font-size: 24rpx;
		}
	}
}
</style>
